package com.example.myapplication.tack_33.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_33.OrderDetailsActivity;
import com.example.myapplication.tack_33.data.CaiDanDetailsData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.google.gson.Gson;

import java.util.List;

public class CaiDanDetailsAdapter extends RecyclerView.Adapter<CaiDanDetailsAdapter.My> {
    private Context context;
    private View view;
    private Gson gson;
    private List<CaiDanDetailsData.DataDTO.OrderInfoDTO.OrderItemListDTO> orderItemList;

    public CaiDanDetailsAdapter(List<CaiDanDetailsData.DataDTO.OrderInfoDTO.OrderItemListDTO> orderItemList,
                                Context context) {
        this.context = context;
        this.orderItemList = orderItemList;
        gson = new Gson();
    }

    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.tack_32_cai_dan,null);
        return new My(view);
    }

    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {
        String productImage = orderItemList.get(position).getProductImage();
        if(!TextUtils.isEmpty(orderItemList.get(position).getProductImage())){
            Glide.with(context).load(OkHttpTool.URL+productImage).into(holder.imgUrl);
        }
        holder.name.setText(orderItemList.get(position).getProductName());
        holder.number.setText("*"+String.valueOf(orderItemList.get(position).getQuantity()));
        int quantity = orderItemList.get(position).getQuantity();
        float productPrice = (float)orderItemList.get(position).getProductPrice();
        holder.amount.setText("总价格："+(quantity*productPrice));
    }

    @Override
    public int getItemCount() {
        return orderItemList != null?orderItemList.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private ImageView imgUrl;
        private TextView amount;
        private TextView name;
        private TextView number;
        public My(@NonNull View itemView) {
            super(itemView);
            imgUrl = view.findViewById(R.id.img);
            name = view.findViewById(R.id.name);
            number = view.findViewById(R.id.number);
            amount = view.findViewById(R.id.amount);
        }
    }
}
